using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Microsoft.Health;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Data;
namespace HVAdapterUnitTestCases
{
    [TestFixture]
    public class AppCodeTestCases
    {
        [Test, Description("Test case for create patient")]
        public void CreatePatientTestCase()
        {
            string identificationCode = string.Empty;
            PatientConnectUtilities createPatient = new PatientConnectUtilities(new Guid("85dbe60d-e191-44f8-b387-59bc5269ae8a"));
            identificationCode = createPatient.CreatePatient("AutomatedPatient", "AutomatedPatient", "AutomatedPatient", Guid.NewGuid().ToString());
            Assert.IsNotEmpty(identificationCode, "Empty Identification Code");
            Assert.AreEqual(24, identificationCode.Length, "Length mismatch for identification code");
        }

        [Test, Description("Test case for create application Id")]
        public void CreateApplicationTestCase()
        {
            Guid newApplicationId = Guid.Empty;
            PatientConnectUtilities createPatient = new PatientConnectUtilities(new Guid("1fbc292c-30be-4dc1-b870-606878e91d03"));
            ApplicationInfo appinfo = new ApplicationInfo();
            X509Certificate cert = new X509Certificate(@"..\..\..\Cert\Master One_ID-1fbc292c-30be-4dc1-b870-606878e91d03.p12");
            appinfo.PublicKeys.Add(cert.GetRawCertData());
            appinfo.Name = "TestApp";
            appinfo.AuthorizationReason = "TestApp";
            appinfo.Description = "TestApp";
            appinfo.DomainName = "contoso.com";
            appinfo.LargeLogo = new ApplicationBinaryConfiguration(@"..\..\..\website\Images\Contoso-Health.jpg", "Image");
            appinfo.PrivacyStatement = new ApplicationBinaryConfiguration(new MemoryStream(UTF8Encoding.Default.GetBytes("TestApp")), "text");
            appinfo.TermsOfUse = new ApplicationBinaryConfiguration(new MemoryStream(UTF8Encoding.Default.GetBytes("TestApp")), "text");
            appinfo.ActionUrl = new Uri("http://www.contoso.com");
            appinfo.ConfigurationOptions = ApplicationOptions.Default;
            List<AuthorizationSetDefinition> authsetDefinition = new List<AuthorizationSetDefinition>();
            authsetDefinition.Add(new TypeIdSetDefinition(Microsoft.Health.ItemTypes.Basic.TypeId));
            AuthorizationRule authrule = new AuthorizationRule(HealthRecordItemPermissions.Read, authsetDefinition, new List<AuthorizationSetDefinition>());
            appinfo.OfflineBaseAuthorizations.Add(authrule);

            newApplicationId = createPatient.CreateApplication(appinfo);
            Assert.AreNotEqual(newApplicationId, Guid.Empty, "Application id not generated");
            Assert.IsInstanceOfType(typeof(Guid), newApplicationId);
            Console.WriteLine("Chid AppId {0}", newApplicationId);
        }

        [Test, Description("Test case for SetAuthorization")]
        public void SetAuthorizationTestCase()
        {
            DataTable authTable = new DataTable();
            //Create Column to authentication Table
            authTable.Columns.Add("Item");
            authTable.Columns.Add("Create", typeof(bool));
            authTable.Columns.Add("Read", typeof(bool));
            authTable.Columns.Add("Update", typeof(bool));
            authTable.Columns.Add("Delete", typeof(bool));
            authTable.Columns.Add("TypeId");

            object[] row = new object[]
            {
                "Basic",
                true,
                true,
                true,
                true,
                Microsoft.Health.ItemTypes.Basic.TypeId
            };
            authTable.Rows.Add(row);
            List<AuthorizationRule> authRuleList = PatientConnectUtilities.SetAuthorizationRule(authTable);
            Assert.AreEqual(4, authRuleList.Count, "AuthorizationRule count mismatch");
        }
    }
}
